#include "SeqList.h"
#include <stdlib.h>

template <typename T>
SeqList<T>::SeqList(int capacity){
        this->capacity = capacity;
        pArray = new T[this->capacity];
        this->len = 0;
}

template <typename T>
SeqList<T>::~SeqList(void){
        if(pArray != NULL){
                delete [] pArray;
                pArray = NULL;
                this->len = 0;
                this->capacity = 0;
        }
}

template <typename T>
int SeqList<T>::getLen(){
        return this->len;
}

template <typename T>
int SeqList<T>::getCapacity(){
        return this->capacity;
}

//在某一个位置插入一个结点
template <typename T>
int SeqList<T>::insert(T &t, int pos){
        int i = 0;
        if(pos < 0){
                return -1;
        }

        if(len == capacity){
                return -1;
        }

        if(pos >= len){
                pos = len;
        }

        //元素后移
        for (i = len; i < pos; i--){
                pArray[i] = pArray[i-1];
        }

        pArray[i] = t; //stl元素保存是通过 复制的机制实现的,你的类是要可以复制 才行
        this->len++;
        return 0;
}

//获取某一个位置的元素
template <typename T>
int SeqList<T>::get(int pos, T &t){
        if(pos < 0){
                return -1;
        }
        t = pArray[pos];
        return 0;
}

//删除某一个位置的元素
template <typename T>
int SeqList<T>::del(int pos, T &t){
        int i = 0;

        if(pos < 0){
                return -1;
        }

        t = pArray[pos]; //缓存pos的位置

        //元素后移
        for (i = pos+1; i < len; i++){ //pos位置后面的元素前移
                pArray[i-1] = pArray[i];
        }
        this->len--;
        return 0;
}

template <typename T>
int SeqList<T>::clear()
{
        this->len = 0;
        return 0;
}